package cn.pid21.client.util;

import cn.pid21.client.exception.DataReportClientException;
import cn.pid21.client.request.DataReportRequest;
import cn.pid21.client.request.DataReportRequestHeader;
import okhttp3.Headers;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * Request 工具类
 *
 * @author 郭志斌
 * @version 1.0.0
 * <b>Creation Time:</b> 2019/8/14 17:18
 */
public final class RequestUtil {

    private RequestUtil(){}

    /**
     * 构建 GET
     *
     * @param headers
     * @param service
     * @return
     */
    public static Request get(Headers headers,String service,String queryName,String queryValue){
        return new Request.Builder()
                .headers(headers)
                .get()
                .url(DataReportRequest.DATA_REPORT_SERVICE_GATEWAY_URL + service + "?" + queryName + "=" + queryValue)
                .build();
    }

    /**
     * 构建 GET
     *
     * @param headers
     * @param service
     * @param param
     * @return
     */
    public static Request get(Headers headers, String service, Map<String,Object> param){
        return get(headers,service,toParam(param));
    }

    /**
     * 构建 GET
     *
     * @param headers
     * @param service
     * @param param
     * @return
     */
    public static Request get(Headers headers, String service, String param){
        return new Request.Builder()
                .headers(headers)
                .get()
                .url(DataReportRequest.DATA_REPORT_SERVICE_GATEWAY_URL + service + param)
                .build();
    }

    /**
     * 构建 POST 请求
     *
     * @param headers
     * @param service
     * @param requestBody
     * @return
     */
    public static Request post(Headers headers,RequestBody requestBody,String service){
        return new Request.Builder()
                .headers(headers)
                .url(DataReportRequest.DATA_REPORT_SERVICE_GATEWAY_URL + service)
                .post(requestBody)
                .build();
    }

    /**
     * 构建 POST 请求
     *
     * @param headers
     * @param service
     * @param requestBody
     * @return
     */
    public static Request post(Headers headers,RequestBody requestBody,String gateway,String service){
        return new Request.Builder()
                .headers(headers)
                .url(gateway + service)
                .post(requestBody)
                .build();
    }

    /**
     * 构建 PUT 请求
     *
     * @param headers
     * @param service
     * @param requestBody
     * @return
     */
    public static Request put(Headers headers,RequestBody requestBody,String service){
        return new Request.Builder()
                .headers(headers)
                .url(DataReportRequest.DATA_REPORT_SERVICE_GATEWAY_URL + service)
                .put(requestBody)
                .build();
    }

    /**
     * 设置请求头
     *
     * @param dataReportRequestHeader
     * @return
     */
    public static Headers headers(DataReportRequestHeader dataReportRequestHeader){
        if(dataReportRequestHeader.getHeader() == null){
            throw new DataReportClientException("请求头未设置");
        }
        return Headers.of(dataReportRequestHeader.getHeader());
    }

    /**
     * Map 转换为 URL GET 请求参数 e.g. ?a=1&b=2
     * @param param
     * @return
     */
    public static String toParam(Map<String,Object> param){
        if (param == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("?");
        for (Map.Entry<String, Object> entry : param.entrySet()) {
            sb.append(entry.getKey() + "=" + entry.getValue());
            sb.append("&");
        }
        String s = sb.toString();
        if (s.endsWith("&")) {
            s = StringUtils.substringBeforeLast(s, "&");
        }
        return s;
    }

}
